<?php
class AjaxController extends CController
{
	public function init(){

	}
	
	public function actionSubscriber(){
		$email = Yii::app()->request->getParam('subscriber', null);
		if(Yii::app()->request->isAjaxRequest && filter_var($email, FILTER_VALIDATE_EMAIL)){
			try{
				Subscribers::getInstance()->insert(array(
					'subscriber_email' => $email, 
					'subscriber_name' => $email, 
					'subscriber_description' => 'From site',
					'subscriber_status' => 1));
			} catch(Exception $ex){
				//keep silent! (duplicate key)	
			}
		}
		die;
	}
	
	public function actionCheckEmail(){
		$email = Yii::app()->request->getParam('email', null);
		if(Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest && filter_var($email, FILTER_VALIDATE_EMAIL)){
			try{
				$user = Users::getInstance()->getByUsername($email);
				if(!empty($user)) echo 1; //email exist
				else echo 0;
			} catch(Exception $ex){
				echo 0;
				//keep silent! (duplicate key)	
			}
		}
		die;
	}
	
	public function actionBanners(){
		$intZoneId = Yii::app()->request->getParam('zone', 1);
		if(Yii::app()->request->isAjaxRequest && !empty($intZoneId)){
			$arrBanners = Advertisements::getInstance()->getListBanners(array('zone_id'=>$intZoneId));
			foreach($arrBanners as $R){
				echo "<img src='".UPLOADS_URL."{$R['banner_file']}'>"	;
			}
		}
		die;
	}
	
	public function actionLike(){
		//check if logged in or not?
		if(!Yii::app()->user->isGuest){
			//get params
			$intPostId = Yii::app()->request->getParam('id', null);
			$intUserId = Yii::app()->user->id;
			if(Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest){
				//0: liked before, 1: sucess
				echo Posts::getInstance()->likePost($intPostId, $intUserId); exit;
			}
		}
		//not login
		echo "-1"; exit;
	}
	
	public function actionComment(){
		//check if logged in or not?
		if(!Yii::app()->user->isGuest){
			//get params
			$intPostId = Yii::app()->request->getParam('post', null);
			$strComment = Yii::app()->request->getParam('comment', null);
			$intUserId = Yii::app()->user->id;
			if(/*Yii::app()->request->isAjaxRequest &&*/ Yii::app()->request->isPostRequest){
				$args = array(
					'comment_body' => $strComment,
					'comment_creator_id' => $intUserId
				);
				echo Posts::getInstance()->insertComment($intPostId, $args); exit;
			}
		}
		//not login
		echo "-1"; exit;
	}
}